/**
 * @className GoodsController
 * @author liuxuelin
 * @Date 2021/1/21
 * @version v1.0
 */

package com.hidt.shop.controller;

import com.hidt.shop.domain.Goods;
import com.hidt.shop.domain.GoodsAndGoods_detail;
import com.hidt.shop.mapper.GoodsMapper;
import com.hidt.shop.service.GoodsService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/goods/add")
    @ResponseBody
    public String addGoodsAll(Goods goods, MultipartFile picture, HttpServletRequest req) throws IOException {
        System.out.println("开始运行");
        if (picture != null) {
           // 指定文件上传后的物理位置
            String path = "F:\\培训笔记\\第二次项目\\img";
            // 获取文件名
            String filename = picture.getOriginalFilename();
            //新的名字
            String new_file_name = UUID.randomUUID() +"-"+ filename;
            File f = new File(path, new_file_name);
            if (!f.exists()) {
                // 上传文件
                FileUtils.copyInputStreamToFile(picture.getInputStream(), new File(req.getServletContext().getRealPath("/images/") + new_file_name));
                picture.transferTo(f);
                goods.setGood_picture(new_file_name);

            }
        }
        int i= goodsService.addGoods(goods);
        if(i > 0){
            return "商品信息添加成功";
        }
        return "商品信息添加失败";
    }

    @RequestMapping("/goods/update")
    @ResponseBody
    public String updateGoodsAll(Goods goods, MultipartFile picture ,HttpServletRequest req) throws IOException {
        System.out.println("开始运行");
        System.out.println(picture);
        System.out.println(goods.getGood_name());
        if (picture != null) {
            // 指定文件上传后的物理位置
            String path = "F:\\培训笔记\\第二次项目\\img";
            // 获取文件名
            String filename = picture.getOriginalFilename();

            System.out.println(filename);
            //新的名字
            String new_file_name = UUID.randomUUID() +"-"+ filename;
            //调用查找的方法找到原图片的文件名
            Goods goodsByGood_id = goodsService.findGoodsByGood_id(goods);
            String picname=goodsByGood_id.getGood_picture();
            //删除修改前的本地图片
            File file=new File(path+"\\"+picname);
            if(file.exists()){
               file.delete();
            }

            File f = new File(path, new_file_name);
            if (!f.exists()) {
                // 上传文件

                System.out.println(req.getServletContext().getRealPath("/images/") + new_file_name);
                FileUtils.copyInputStreamToFile(picture.getInputStream(), new File(req.getServletContext().getRealPath("/images/") + new_file_name));
                picture.transferTo(f);
                goods.setGood_picture(new_file_name);

            }
        }
        int i= goodsService.updateGoodsByGood_id(goods);
        if(i > 0){
            return "商品信息修改成功";
        }
        return "商品信息修改失败";
    }

    @RequestMapping("/goods/updatebyid")
    @ResponseBody
    public String updatebyid(@RequestBody Map<String,String> map){
        int good_id = Integer.parseInt(map.get("good_id"));
        System.out.println(good_id);
        int i = goodsService.updateById(good_id);

        if(i > 0){
            return "商品信息删除成功";
        }
        return "商品信息删除失败";

    }
    /**
     *@方法功能描述：查询所有的商品
     *@methodName findAll
     *@params []
     *@return java.util.List<com.hidt.shop.domain.Goods>
     *@author 刘雪林
     *@Date 2021/1/25
     */
    @RequestMapping("/goods/all")
    @ResponseBody
    public List<Goods> findAll(){
        List<Goods> all = goodsService.findAll();
        return all;
    }
    /**
     *@方法功能描述：查询回收站商品
     *@methodName findAll2
     *@params []
     *@return java.util.List<com.hidt.shop.domain.Goods>
     *@author 刘雪林
     *@Date 2021/1/25
     */
    @RequestMapping("/goods/all2")
    @ResponseBody
    public List<Goods> findAll2(){
        List<Goods> all = goodsService.findAll2();
        return all;
    }
    /**
     *@方法功能描述：根据id查找
     *@methodName findById
     *@params [map]
     *@return java.util.List<com.hidt.shop.domain.Goods>
     *@author 刘雪林
     *@Date 2021/1/26
     */
    @RequestMapping("/goods/find")
    @ResponseBody
    public  List<Goods> findById(@RequestBody Map<String,String> map){
        System.out.println(map.get("good_id"));
        List<Goods> goodall = goodsService.findById(Integer.parseInt(map.get("good_id")) );
        return  goodall;
    }
}
